<?php
 include('../../Connections/KRAPPERCON.php');
 $cini = $_POST['t_ro_cini'];
 $cfin = $_POST['t_ro_cfin'];
 $dini = $_POST['t_ro_dini'];
 $dfin = $_POST['t_ro_dfin'];
 $usr  = $_POST['t_ro_usr'];;
 $sdet1 = '';
 $sdet2 = '';
 $f_det = getVal('filter_det');
 $cart = getVal('t_oc_cart');
 $aalm = getVal('cbo_alm');
 $asts = getVal('oc_sts');
 
// $alm = '';
 $pro = $_POST['cbo_ro_pro'];;
//$cen = $_POST['cbo_ro_cen'];

$str_usr = '';
$tsr_cini= '';
$str_cfin = '';
$str_dini = '';
$str_dfin = '';

$str_art = '';
$str_alm = '';
$str_sts = '';
$sdet3 = 'OrdenMonto';
if ( $f_det == '1'){
$sdet3 = 'DetPreNet';
$sdet1 = ',po.ArticuloID,po.AlmacenID,po.DetOrdQtp,po.DetOrdQta,DetPreNet';
$sdet2 =' inner join tt_detordcom po on po.OrdenNro = oc.OrdenNro ';
// Agregar FIltros a nivel de Detalle
$str_art = isNull($cart," and po.ArticuloID = '".$cart."'");
$str_alm = isNull($aalm," and po.AlmacenID = '".$aalm."'");
	if ($asts == '1') {
	$str_sts = " and po.DetOrdQtp > po.DetOrdQta";
	}
	else if($asts == '2'){
	$str_sts = " and po.DetOrdQtp = po.DetOrdQta";
	}
}

$str_pro = isZeros($pro,"and pr.ProveedID=$pro");
//$str_alm = isZeros($alm,"and 
$str_usr = isNull($usr,"and oc.UserID='".$usr."'");

if (!isEmpty($cfin)){
$str_cini = isNull($cini," and date(oc.OrdenFecCre) >= '".$cini."'");
$str_cfin = isNull($cfin," and date(oc.OrdenFecCre) <= '".$cfin."'");
}
else {
$str_cini = isNull($cini," and date(oc.OrdenFecCre) = '".$cini."'");
}

if (!isEmpty($dfin)){
$str_dini = isNull($dini," and oc.OrdenFecDoc >= '".$dini."'");
$str_dfin = isNull($dfin," and oc.OrdenFecDoc <= '".$dfin."'");
}
else {
$str_dini = isNull($dini," and oc.OrdenFecDoc = '".$dini."'");
}
$str_cen = '';
//$str_cen = isZeros($cen," oc.CentroID = ".$cen);
$where = '';
$cond = ($str_pro." "." ".$str_art." ".$str_alm." ".$str_cen." ".$str_usr." ".$str_cini. "  ".$str_cfin." " .$str_dini. " " . $str_dfin." ".$str_sts);
if (!trim($cond)=='' ){
$cond = ltrim($cond);
$where = "where ".substr($cond,3);
}



 
 $sql = "SELECT
oc.OrdenNro,
oc.OrdenFecDoc,
oc.UserID,
oc.ProveedID,
pr.ProveedNom,
oc.OrdenFecCre,
oc.OrdenFecEnt,
oc.OrdenMonto,
oc.MoneyID,
oc.OrdenFecApr,
oc.OrdenUsrApr,
oc.ClaDocID,
oc.OrdenLibre " . $sdet1 ."
FROM tt_ordcompra as oc inner join tt_proveedor AS pr 
on oc.proveedID = pr.proveedID ".$sdet2 .$where." order by OrdenNro ";
 doEcho($sql); 
?>
<table width="100%" rules="all" border="1" cellspacing="0" cellpadding="0" id="tbl_qoc" class="tblSort">
<thead>
  <tr>
    <th>Nro de Orden</th>
    <th>F.Creacion</th>
        <th>F.Documento</th>
        <th>Fecha Entrega</th>
    <th>Proveedor</th>
    <th>Monto</th>
    <th>Creador</th>
    <?php if ($f_det == '1') { ?>
    <th>Articulo</th>
        <th>Almacen</th>
         <th>Ctd.Ped.</th>
          <th>Ctd.Ate.</th>
	<?php 
	}
	?>
<!--    <th>Liberacion</th> -->
  </tr>
  </thead>
  <tbody>
  <?php 

//$sql = 'call sp_getAll_OC();';
$res = $mysqli->query($sql);
$tot = 0;
if ($res){
while( $row = $res->fetch_assoc()){
//$tot += $row['OrdenMonto'];
  ?>
  <tr>
    <td><a class="a" href="#"><?php echo $row['OrdenNro'];?></a></td>
    <td><?php echo $row['OrdenFecCre'];?></td>
	    <td><?php echo $row['OrdenFecDoc'];?></td>
        <td><?php echo $row['OrdenFecEnt'];?></td>
    <td><?php echo $row['ProveedID']."/".$row['ProveedNom'];?></td>
    <td>
      <div align="right"><?php echo $row[$sdet3]?></div></td>
    <td><?php echo $row['UserID']?></td>
     <?php if ($f_det == '1') { ?>
    <td><?php echo $row['ArticuloID']?></td>
        <td><?php echo $row['AlmacenID']?></td>
         <td class="qt"><?php echo $row['DetOrdQtp']?></td>
          <td class="qt"><?php echo $row['DetOrdQta']?></td>
	<?php 
	}
	?>
    <!-- <td><?php if ($row['OrdenLibre']==1) echo 'No'; else 'Si'; ?></td> -->
  </tr>
  <?php 
  }
  $res->close();
 $mysqli->check_result(); 
  }
  ?>
  </tbody>
  <tfoot>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>  
      <?php if ($f_det == '1') { ?>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td> 
	<?php 
	}
	?>
  </tr>
  </tfoot>
</table>
<?php 

?>
<script type="text/javascript">
$("#tbl_qoc tbody .a").each(function(){
	this.href = '#page/Procesos/MantOC.php?orden='+this.innerHTML;
});
</script>